/*
 * JHeuristics - The Java Meta-Heuritics Library
 * Copyright (C) 2004-2005, Universidade Federal de Campina Grande. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.jheuristics.ga;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.jheuristics.Individual;
import org.jheuristics.util.InvertNaturalOrderComparator;

/**
 * TODO
 *
 * @author Marcell Manfrin, marcell@ourgrid.org, Jan 9, 2006
 */
public class DefaultPopulation implements Population, Serializable {
	private List population;
	
	/**
	 * TODO 
	 */
	public DefaultPopulation() {
		population = createList();
	}

	/**
	 * TODO
	 *
	 * @param toAdd
	 * @see org.jheuristics.ga.Population#addIndividual(org.jheuristics.Individual)
	 */
	public void addIndividual(Individual toAdd) {
		if (null == toAdd) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		population.add(toAdd);
	}

	/**
	 * TODO
	 *
	 * @param individuals
	 * @see org.jheuristics.ga.Population#addIndividuals(org.jheuristics.Individual[])
	 */
	public void addIndividuals(Individual[] individuals) {
		if (null == individuals) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		for (int i = 0; i < individuals.length; i++) {
			if (null == individuals[i]) {
				// TODO: ERROR MSG
				throw new NullPointerException();
			}
		}
		for (int i = 0; i < individuals.length; i++) {
			population.add(individuals[i]);
		}
	}

	/**
	 * TODO
	 *
	 * @param population
	 * @see org.jheuristics.ga.Population#addIndividuals(org.jheuristics.ga.Population)
	 */
	public void addIndividuals(Population population) {
		if (null == population) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		Iterator it = population.iterator();
		if (null == it) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		for (; it.hasNext(); ) {
			if (null == it.next()) {
				// TODO: ERROR MSG
				throw new NullPointerException();
			}
		}
		it = population.iterator();
		while (it.hasNext()) {
			addIndividual((Individual) it.next());
		}
	}

	/**
	 * TODO
	 *
	 * @param collection
	 * @see org.jheuristics.ga.Population#addAll(java.util.Collection)
	 */
	public void addAll(Collection collection) {
		if (null == collection) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		Iterator it = collection.iterator();
		if (null == it) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		for (; it.hasNext(); ) {
			Object obj = it.next();
			if (!(obj instanceof Individual) && !(obj instanceof Population)) {
				if (null == obj) {
					// TODO: ERROR MSG
					throw new NullPointerException();
				} else {
					// TODO: ERROR MSG
					throw new ClassCastException();
				}
			}
		}
		population.addAll(collection);
	}

	/**
	 * TODO
	 *
	 * @param toRemove
	 * @see org.jheuristics.ga.Population#removeIndividual(org.jheuristics.Individual)
	 */
	public void removeIndividual(Individual toRemove) {
		if (null == toRemove) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		population.remove(toRemove);
	}

	/**
	 * TODO
	 *
	 * @param individuals
	 * @see org.jheuristics.ga.Population#removeIndividuals(org.jheuristics.Individual[])
	 */
	public void removeIndividuals(Individual[] individuals) {
		if (null == individuals) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		for (int i = 0; i < individuals.length; i++) {
			if (null == individuals[i]) {
				// TODO: ERROR MSG
				throw new NullPointerException();
			}
		}
		for (int i = 0; i < individuals.length; i++) {
			population.remove(individuals[i]);
		}
	}

	/**
	 * TODO
	 *
	 * @param population
	 * @see org.jheuristics.ga.Population#removeIndividuals(org.jheuristics.ga.Population)
	 */
	public void removeIndividuals(Population population) {
		if (null == population) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		Iterator it = population.iterator();
		if (null == it) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		for (; it.hasNext(); ) {
			if (null == it.next()) {
				// TODO: ERROR MSG
				throw new NullPointerException();
			}
		}
		it = population.iterator();
		while (it.hasNext()) {
			removeIndividual((Individual) it.next());
		}
	}

	/**
	 * TODO
	 *
	 * @param index
	 * @see org.jheuristics.ga.Population#removeIndividual(int)
	 */
	public void removeIndividual(int index) {
		if (0 > index) {
			// TODO: ERROR MSG
			throw new IndexOutOfBoundsException();
		}
		if (population.size() <= index) {
			// TODO: ERROR MSG
			throw new IndexOutOfBoundsException();
		}
		population.remove(index);
	}

	/**
	 * TODO
	 *
	 * @param collection
	 * @see org.jheuristics.ga.Population#removeAll(java.util.Collection)
	 */
	public void removeAll(Collection collection) {
		if (null == collection) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		Iterator it = collection.iterator();
		if (null == it) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		for (; it.hasNext(); ) {
			Object obj = it.next();
			if (!(obj instanceof Individual) && !(obj instanceof Population)) {
				if (null == obj) {
					// TODO: ERROR MSG
					throw new NullPointerException();
				} else {
					// TODO: ERROR MSG
					throw new ClassCastException();
				}
			}
		}
		population.removeAll(collection);
	}

	/**
	 * TODO
	 *
	 * @param index
	 * @return
	 * @see org.jheuristics.ga.Population#getIndividual(int)
	 */
	public Individual getIndividual(int index) {
		return (Individual) population.get(index);
	}

	/**
	 * TODO
	 *
	 * @see org.jheuristics.ga.Population#clear()
	 */
	public void clear() {
	    population.clear();
	}

	/**
	 * TODO
	 *
	 * @return
	 * @see org.jheuristics.ga.Population#size()
	 */
	public int size() {
		return population.size();
	}

	/**
	 * TODO
	 *
	 * @return
	 * @see org.jheuristics.ga.Population#iterator()
	 */
	public Iterator iterator() {
		return population.iterator();
	}

	/**
	 * TODO
	 *
	 * @return
	 * @see org.jheuristics.ga.Population#toIndividualArray()
	 */
	public Individual[] toIndividualArray() {
		Individual[] individuals = new Individual[population.size()];
		Iterator it = population.iterator();
		for (int i = 0 ; i < individuals.length; i++) {
			individuals[i] = (Individual) it.next();
		}
		return individuals;
	}

	/**
	 * TODO
	 *
	 * @param individual
	 * @return
	 * @see org.jheuristics.ga.Population#contains(org.jheuristics.Individual)
	 */
	public boolean contains(Individual individual) {
		if (null == individual) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		return population.contains(individual);
	}

	/**
	 * TODO
	 *
	 * @see org.jheuristics.ga.Population#sort()
	 */
	public void sort() {
		sort(new InvertNaturalOrderComparator());
	}

	/**
	 * TODO
	 *
	 * @param comparator
	 * @see org.jheuristics.ga.Population#sort(java.util.Comparator)
	 */
	public void sort(Comparator comparator) {
		Collections.sort(population, comparator);
	}

    /**
     * TODO
     *
     * @param that
     * @return
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object that) {
        if (this == that)
            return true;
        if (that == null || that.getClass() != this.getClass()) 
            return false;
        DefaultPopulation other = (DefaultPopulation) that;
        return this.population.equals(other.population);
    }

     /**
	 * TODO
	 * 
	 * @return
	 */
	private List createList() {
		return new LinkedList();
	}

}
